package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.BanJi;
import com.example.entity.XueYuan;
import com.example.service.BanJiService;
import com.example.service.XueYuanService;
import com.example.vo.BanJiVo;
import com.example.vo.DataView;
import com.example.vo.XueYuanVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BanJiController {

    @Autowired
    private BanJiService banJiService;

    @RequestMapping("/banji/listBanJi")
    public DataView listBanJi(BanJiVo banJiVo){
        IPage<BanJi> page=new Page<>(banJiVo.getPage(), banJiVo.getLimit());
        QueryWrapper<BanJi> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(banJiVo.getName()),"name",banJiVo.getName());
        banJiService.page(page,queryWrapper);
        DataView dataView=new DataView(page.getTotal(),page.getRecords());
        return dataView;
    }


    @RequestMapping("/banji/deleteById")
    public DataView deleteById(Integer id){
        banJiService.removeById(id);
        DataView dataview=new DataView();
        dataview.setCode(200);
        dataview.setMsg("删除数据成功");
        return dataview;
    }

    @RequestMapping("/banji/addOrUpdateBanJi")
    public DataView addOrUpdateBanJi(BanJi banJi){
        boolean save = banJiService.saveOrUpdate(banJi);
        DataView dataview=new DataView();
        if(save){
            dataview.setCode(200);
            dataview.setMsg("新增数据成功");
            return dataview;
        }
        dataview.setCode(100);
        dataview.setMsg("操作失败");
        return dataview;
    }
}
